package com.answerz.module_reportissues.viewmodel

import android.os.Bundle
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.OrderDetailBean
import com.czl.base.data.bean.RepairListBean
import com.czl.base.data.bean.ReportListBean
import com.czl.base.data.bean.ReportOrderState
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.luck.picture.lib.entity.LocalMedia

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/10 11:28
 */
class ReportListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 0
    var ownerState = "0"
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<ReportListBean> = SingleLiveEvent()
        val cancelOrderEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val deleteOrderEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val navScoreEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val navPayEvent: SingleLiveEvent<String> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getMonthCardList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 0
        getMonthCardList()
    })

    private fun getMonthCardList() {
        model.getRepairList(
            mapOf(
                "areaNo" to model.getAreaId(),
                "pageNum" to (currentPage + 1).toString(),
                "pageSize" to "10",
                "state" to ownerState
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ReportListBean>>() {
                override fun onResult(t: BaseBean<ReportListBean>) {
                    if (t.code == 200) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }

    fun cancelWorkOrder(no: String) {
        model.cancelWorkOrder(no)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    dismissLoading()
                    if (t.code == 200) {
                        uc.refreshListEvent.call()
                        showSuccessToast("撤销成功！")
                    } else {
                        showSuccessToast("撤销失败！")
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    fun deleteWorkOrder(no: String) {
        model.deleteWorkOrder(no)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    dismissLoading()
                    if (t.code == 200) {
                        uc.refreshListEvent.call()
                        showSuccessToast("删除成功！")
                    } else {
                        showSuccessToast("删除失败！")
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    /**
     * 获取订单金额，然后跳转支付页面
     */
    fun getFree(no: String) {
        model.getOrderDetail(no)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<OrderDetailBean>>() {
                override fun onResult(t: BaseBean<OrderDetailBean>) {
                    dismissLoading()
                    if (t.code == 200) {
                        t.data?.let {
                            var payInfo = GsonUtils.toJson(
                                mapOf(
                                    "no" to no,
                                    "payAmount" to it.serviceFee.toString(),
                                    "areaId" to model.getAreaId()
                                )
                            )
                            startContainerActivity(
                                AppConstants.Router.Pay.F_PAY_DETAIL,
                                Bundle().apply {
                                    putString(AppConstants.BundleKey.PAY_ORDER_KEY, payInfo)
                                    putString(
                                        AppConstants.BundleKey.WORK_ORDER_IS,
                                        AppConstants.Constants.IS_WORK_ORDER
                                    )
                                })
                        }
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}